package exam.web.action;

import exam.business.service.SubQuestionGetService;
import exam.business.service.SubQuestionUpdateService;
import exam.common.key.KeySubQuestion;
import exam.common.util.NullCheckUtil;
import exam.model.entity.TSubQuestionEntity;
import exam.web.action.base.SubQuestionActionBase;

/**
 * <p>EditSubQuestionAction</p>
 * 
 * @author Phong
 * @version (create: 09/05/2012) (update: 20/05/2012)
 */
public class EditSubQuestionAction extends SubQuestionActionBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		String port = null;
		TSubQuestionEntity tSubQuestionEntity = null;
		String btUpdate = request.getParameter("btUpdate");
		if(NullCheckUtil.isNull(btUpdate)) {
			// lay subQuestId
			String subQuestId = request.getParameter("subQuestId");
			if(NullCheckUtil.isNull(subQuestId)) {
				addActionError("Thao tác không hợp lệ. Vui lòng kiểm tra lại");
				return ERROR;
			}
			// put subQuestId vao serviceInfo
			serviceInfo.put(KeySubQuestion.SUB_QUEST_ID, subQuestId);
			// yeu cau lay thong tin danh muc cau hoi
			SubQuestionGetService subQuestionGetService = new SubQuestionGetService();
			port = subQuestionGetService.executeService(serviceInfo);
			if(NONE.equals(port)) {
				return NONE;
			} else {
				tSubQuestionEntity = serviceInfo.getObject(KeySubQuestion.T_SUB_QUESTION_ENTITY);
				copyFromTSubQuestionEntity(tSubQuestionEntity);
				return INPUT;
			}
		} else {
			// copy thuoc tinh
			copyToTSubQuestionEntity(tSubQuestionEntity);
			// put tSubQuestionEntity vao serviceInfo
			serviceInfo.put(KeySubQuestion.T_SUB_QUESTION_ENTITY, tSubQuestionEntity);
			// yeu cau cap nhat thong tin danh muc cau hoi
			SubQuestionUpdateService subQuestionUpdateService = new SubQuestionUpdateService();
			port = subQuestionUpdateService.executeService(serviceInfo);
			if(FAIL.equals(port)) {
				addActionError("Cập nhật danh mục câu hỏi thất bại");
			} else {
				addActionMessage("Cập nhật danh mục câu hỏi thành công");
			}
		}
		return port;
	}

}
